package com.sdleyou.treepro.common.service.impl;

import com.sdleyou.treepro.common.exception.ServiceException;
import com.sdleyou.treepro.common.model.dao.CardDO;
import com.sdleyou.treepro.common.model.dao.PropDO;
import com.sdleyou.treepro.common.model.dto.PropDTO;
import com.sdleyou.treepro.common.model.enums.ErrorCodeEnum;
import com.sdleyou.treepro.common.model.utils.RandomUtil;
import com.sdleyou.treepro.common.model.vo.CardVO;
import com.sdleyou.treepro.common.model.vo.PropVO;
import com.sdleyou.treepro.common.service.CardManager;
import com.sdleyou.treepro.common.service.MongoPageManager;
import com.sdleyou.treepro.common.service.PropManager;
import com.sdleyou.treepro.common.utils.DateUtil;
import com.sdleyou.treepro.common.utils.IdGen;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * 道具管理实现类
 */
@Service
public class CardManagerImpl implements CardManager {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private MongoPageManager mongoPageManager;

    @Override
    public void createCard(String img) {
        CardDO cardDO = new CardDO();
        cardDO.setCardId(IdGen.uuid());
        cardDO.setImg(img);
        cardDO.setGmtCreate(DateUtil.getDateline());
        cardDO.setGmtModified(DateUtil.getDateline());
        mongoTemplate.insert(cardDO);
    }

    @Override
    public void updateCard(String cardId, String img) {
        Query query = new Query(Criteria.where("card_id").is(cardId));
        CardDO cardDO = mongoTemplate.findOne(query, CardDO.class);
        if (cardDO == null) {
            throw new ServiceException(ErrorCodeEnum.E700.code(), "卡片不存在");
        }
        Update update = new Update();
        update.set("img", img);
        update.set("gmt_modified", DateUtil.getDateline());
        mongoTemplate.updateFirst(query, update, PropDO.class);
    }

    @Override
    public CardVO findOne(String cardId) {
        Query query = new Query(Criteria.where("card_id").is(cardId));
        CardDO cardDO = mongoTemplate.findOne(query, CardDO.class);
        CardVO cardVO = new CardVO();
        if(cardDO!=null) {
            BeanUtils.copyProperties(cardDO, cardVO);
        }
        return cardVO;
    }

    @Override
    public CardVO random() {
        List<CardDO> cards = mongoTemplate.findAll(CardDO.class);
        CardVO cardVO = new CardVO();
        if(cards!=null && cards.size()>0){
            Random random = new Random();
            int pos = random.nextInt(cards.size() - 1);
            CardDO cardDO = cards.get(pos);
            BeanUtils.copyProperties(cardDO, cardVO);
        }
        return cardVO;
    }

}
